<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\api\model;

use think\Exception;
use \think\Model;
use think\db;
class Order extends Model
{
    public function goods(){

        return $this->belongsTo('Goods');
    }

    public function member(){

        return $this->belongsTo('Member');
    }
    public function cancel($t=''){
        $a='';
        if ($t!=''){
            $b=  $this->where('order_id',$t)->update([
                'order_status'=>6
            ]);
            if ($b==true){
                $a=true;
                return $a;
            }else{
                $a=false;
                return $a;
            }

        }else{
            $a=false;
            return $a;
        }
    }
    //下单返佣金 未入账的记录
    public function shareCashBackToSpreadMember($post,$order)
    {
        //没有推广人直接返回
        if(empty($post['spread_member_id'])) return false;
        $shareCashBackRate = config('shareCashBackRate');
        $salePrice = $post['amount']??0;
        $res = db('distributiondetails')->insert([
            'member_id'=>$post['spread_member_id']??0,
            'money'=>$salePrice*$shareCashBackRate,
            'consumption_money'=>$salePrice,
            'consumption_time'=>time(),
            'consumption_goods'=>$order['line']??0,
            'consumption_member_username'=>$order['personalOrder']['booker']['username']??'',
            'consumption_member_id'=>$post['member_id']??0,
            'consumption_order_id'=>$order['personalOrder']['orderNo'],
            'order_type'=>$order['personalOrder']['orderType'],
            'order_name'=>$order['carPoolOrder']['name'],
            'enter_account'=>0,
        ]);

        return $res;
    }

    //订单完成 将预计佣金加入用户佣金
    public function addPromoteMemberMoneyByConfirmOrder($orderNo)
    {
        //查询未入账记录
        $orderLog = db('distributiondetails')
            ->where(['consumption_order_id'=>$orderNo,'enter_account'=>0])
            ->find();
        if(empty($orderNo) || empty($orderLog) || empty($orderLog['member_id'])) return '未查询到该返佣记录';
        db::startTrans();
        try{
            //1 修改入账状态
            db('distributiondetails')->where('id',$orderLog['id'])->update([
                'enter_account'=>1
            ]);
            //2 加入佣金收入
            db('member')->where('member_id',$orderLog['member_id'])->setInc('member_money',floatval($orderLog['money']));
            //3 记录佣金收入流水
            db('accounts')->insert([
                'member_id'=>$orderLog['member_id'],
                'admin_id'=>0,
                'order_no'=>$orderLog['consumption_order_id'],
                'order_type'=>$orderLog['order_type'],
                'accounts_money'=>$orderLog['money'],
                'description'=>'推荐'.$orderLog['consumption_member_username'].'用户下单，返现'.$orderLog['money'],
                'business_way'=>3,
                'creattime'=>date('YmdHis')
            ]);

            db::commit();
            return ['msg'=>'更新用户佣金成功'];
        }catch(Exception $e){
            db::rollback();
            return $e->getMessage();
        }

    }
    //拼友订单确认 记录用户的预计收益
    public function addExpextMoneyByCompleteGrouptourOrder($orders)
    {
        db::startTrans();
        try{
            foreach ($orders as $order){
                if(empty($order['member_id'])) continue;
                $orderNo = $order['consumption_order_id']??'';
                $member = db('member')->find($order['member_id']);
                $exist = db('distributiondetails')->where(['member_id'=>$order['member_id'],'consumption_order_id'=>$orderNo])->find();
                if(!empty($exist)) continue;//已经记录过的订单号 略过【防止重复】
                //记录返现的预计收益
                $paid_time = $order['paid_time']??date('Y-m-d H:i:s');
                $paid_time = strtotime($paid_time);
                if(!empty($order['line_id'])){
                    $grouptour = db('group_tours_line')->find($order['line_id']);
                }
                db('distributiondetails')->insert([
                    'member_id'=>$order['member_id'],
                    'money'=>$order['money'],
                    'consumption_money'=>$order['fold_money']??0,
                    'consumption_time'=>$paid_time,
                    'consumption_goods'=>$order['line_id']??0,
                    'consumption_member_username'=>$member['member_nickname']??'',
                    'consumption_member_id'=>$order['member_id'],
                    'consumption_order_id'=>$orderNo,
                    'order_type'=>$order['order_type']??'',
                    'order_name'=>$grouptour['title']??'用户拼团订单完成返现',
                    'enter_account'=>0,
                    'cash_back'=>1,//拼游返现
                ]);
            }
            db::commit();
            return ['msg'=>'更新用户拼游返现金额成功'];
        }catch(Exception $e){
            db::rollback();
            return $e->getMessage();
        }
    }
    //拼游订单完成 所有用户返现金额入账
    public function addMemeberMoneyByConfirmGrouptourOrder($orders)
    {
        db::startTrans();
        try{
            foreach ($orders as $order){
                if(empty($order['member_id'])) continue;
                $orderNo = $order['consumption_order_id']??'';
                $exist = db('distributiondetails')->where(['member_id'=>$order['member_id'],'consumption_order_id'=>$orderNo,'enter_account'=>0])->find();
                if(empty($exist)) continue;
                $addMoney = $exist['money']??0;
                //1 加入佣金收入
                db('member')->where('member_id',$order['member_id'])->setInc('member_money',floatval($addMoney));
                //2 记录佣金收入流水
                db('accounts')->insert([
                    'member_id'=>$order['member_id'],
                    'admin_id'=>0,
                    'order_no'=>$orderNo??'',
                    'order_type'=>$order['order_type']??'',
                    'accounts_money'=>$addMoney??0,
                    'description'=>'订单：'.$orderNo.'出行完成，返现金额'.$addMoney,
                    'business_way'=>3,
                    'creattime'=>date('YmdHis')
                ]);
                //3 收益入账
                db('distributiondetails')->where('id',$exist['id'])->update([
                    'enter_account'=>1
                ]);
            }

            db::commit();
            return ['msg'=>'用户拼游返现金额入账成功'];
        }catch(Exception $e){
            db::rollback();
            return $e->getMessage();
        }
    }
}
